﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//
using System.Data.OleDb;
using System.Data;
using System.Windows.Forms;
using CommonBase;
using System.Diagnostics;

namespace DBLib
{
    /// <summary>
    /// 数据操作类【基类】，其它 DAL 须继承于此
    /// </summary>
    public class DataOperation
    {
        #region 变量

        DataConn dataCon = new DataConn(); // 连接数据库
        OleDbConnection oleDbCon; // 数据库连接

        #endregion

        #region 方法

        /// <summary>
        /// 执行 SQL 语句
        /// </summary>
        /// <param name="cmdText">查询的文本</param>
        /// <param name="connectionStringName">ConnectionString Name</param>
        /// <param name="mainProjectEXE">主工程可执行文件名称</param>
        /// <returns></returns>
        public int RunSqlCommand(string cmdText,
            string connectionStringName, string mainProjectEXE)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(connectionStringName) ||
                    string.IsNullOrWhiteSpace(mainProjectEXE))
                {
#if DEBUG
                    Debug.WriteLine("连接字符串数据库名称 | 主工程可执行文件名称 不正确！");
#endif
                    return 0;
                }

                OleDbCommand oleDbCom = GetDbCommand(cmdText,
                    connectionStringName, mainProjectEXE);
                oleDbCon.Open();
                return oleDbCom.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ConstantString.MSG_COMPANY);
                return 0;
            }
            finally
            {
                if (oleDbCon.State == ConnectionState.Open)
                {
                    oleDbCon.Close();
                }
            }
        }

        /// <summary>
        /// 获取数据集
        /// </summary>
        /// <param name="cmdText">查询的文本</param>
        /// <param name="tbName">数据表名称</param>
        /// <param name="connectionStringName">ConnectionString Name</param>
        /// <param name="mainProjectEXE">主工程可执行文件名称</param>
        /// <returns></returns>
        public DataSet GetDataSet(string cmdText, string tbName,
            string connectionStringName, string mainProjectEXE)
        {
            DataSet dataSet = new DataSet();

            try
            {
                if (string.IsNullOrWhiteSpace(connectionStringName) ||
                    string.IsNullOrWhiteSpace(mainProjectEXE))
                {
#if DEBUG
                    Debug.WriteLine("连接字符串数据库名称 | 主工程可执行文件名称 不正确！");
#endif
                    return dataSet;
                }

                OleDbDataAdapter oleDbAdapter = GetDataAdapter(cmdText,
                    connectionStringName, mainProjectEXE);

                if (string.IsNullOrWhiteSpace(tbName))
                    oleDbAdapter.Fill(dataSet);
                else
                    oleDbAdapter.Fill(dataSet, tbName);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ConstantString.MSG_COMPANY);
            }

            return dataSet;
        }

        /// <summary>
        /// 获取 DbCommand
        /// </summary>
        /// <param name="cmdText">查询的文本</param>
        /// <param name="connectionStringName">ConnectionString Name</param>
        /// <param name="mainProjectEXE">主工程可执行文件名称</param>
        /// <returns></returns>
        public OleDbCommand GetDbCommand(string cmdText,
            string connectionStringName, string mainProjectEXE)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(connectionStringName) ||
                    string.IsNullOrWhiteSpace(mainProjectEXE))
                {
#if DEBUG
                    Debug.WriteLine("连接字符串数据库名称 | 主工程可执行文件名称 不正确！");
#endif
                    return null;
                }

                oleDbCon = dataCon.GetOleDbConnection(connectionStringName, mainProjectEXE);
                return new OleDbCommand(cmdText, oleDbCon);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ConstantString.MSG_COMPANY);
                return null;
            }
        }

        /// <summary>
        /// 获取 SqlDataAdapter
        /// </summary>
        /// <param name="cmdText">查询的文本</param>
        /// <param name="connectionStringName">ConnectionString Name</param>
        /// <param name="mainProjectEXE">主工程可执行文件名称</param>
        /// <returns></returns>
        public OleDbDataAdapter GetDataAdapter(string cmdText,
            string connectionStringName, string mainProjectEXE)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(connectionStringName) ||
                    string.IsNullOrWhiteSpace(mainProjectEXE))
                {
#if DEBUG
                    Debug.WriteLine("连接字符串数据库名称 | 主工程可执行文件名称 不正确！");
#endif
                    return null;
                }

                oleDbCon = dataCon.GetOleDbConnection(connectionStringName, mainProjectEXE);
                return new OleDbDataAdapter(cmdText, oleDbCon);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ConstantString.MSG_COMPANY);
                return null;
            }
        }

        #endregion
    }
}
